/**
 * Used to reverse a TestCaseContainer to a class
 */
package unittest;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import multithread.framework.annotation.ThreadAfter;
import multithread.framework.annotation.ThreadAllAfter;
import multithread.framework.annotation.ThreadAllBefore;
import multithread.framework.annotation.ThreadBefore;
import multithread.framework.annotation.ThreadFor;
import multithread.framework.annotation.ThreadGroupAfter;
import multithread.framework.annotation.ThreadGroupBefore;
import multithread.framework.threadmethod.IThreadMethod;
import multithread.framework.threadmethod.TestCaseContainer;
import multithread.framework.threadmethod.ThreadMethod;
import multithread.framework.threadmethod.ThreadMethodContainer;
import multithread.framework.threadmethod.ThreadMethodGroup;

/**
 * @author russell_wang
 * @since 2011/12/28
 */
public class ReverseToClass {
	
	ThreadMethodContainer methodContainer=null;
	
	public void reDisplay(TestCaseContainer testCaseContainer) {
		
		for(Iterator<ThreadMethodContainer> iterator=testCaseContainer.iterator();iterator.hasNext();) {
			methodContainer=iterator.next();
			Class<?> clazz = methodContainer.getTestCaseClass();
			
			StringBuilder initClazz = new StringBuilder();
			//public class
			initClazz.append("public class ");
			//public class className
			initClazz.append(clazz.getSimpleName());
			initClazz.append(" {\n\n   ");
			
			Field[] fields = clazz.getDeclaredFields();
			for(Field field:fields) {
				initClazz.append(Modifier.toString(field.getModifiers())+" ");
				initClazz.append(field.getType()+" ");
				initClazz.append(field.getName()+";\n\n    ");
			}
			
			List<IThreadMethod> allMethodList=getAllMethods(methodContainer);
			for(int i=0;i<allMethodList.size();i++) {
				ThreadMethod threadMethod=(ThreadMethod)allMethodList.get(i);
				Method method = threadMethod.getIvokeMthod();
				
				Annotation[] annotations=method.getAnnotations();
				for(Annotation annotationClazz:annotations) {
					initClazz.append("@");
					initClazz.append(annotationClazz.annotationType().getSimpleName());
					initClazz.append("\n    ");
				}
				
				// public\private\protected
				String modifer=Modifier.toString(method.getModifiers());
				initClazz.append(modifer);
				initClazz.append(" ");
				//return type
				String returnType=method.getReturnType().toString();
				initClazz.append(returnType);
				initClazz.append(" ");
				// method name
				String methodName=method.getName();
				initClazz.append(methodName);
				initClazz.append("(");
				Class<?>[] paramTypes=method.getParameterTypes();
				for(Class<?> typeClazz:paramTypes) {
					initClazz.append(typeClazz.getName());
					initClazz.append(" ");
				}
				initClazz.append(") {\n\n    }\n\n    ");
			}
			initClazz.append("}");
			System.out.println(initClazz);
		}
	}
	
	@SuppressWarnings("null")
	public List<IThreadMethod> getAllMethods(ThreadMethodContainer methodContainer) {
		
		Class<? extends Annotation> threadBeforeClzs=null;
		Class<? extends Annotation> threadAfterClzs=null;
		Class<? extends Annotation> threadGroupBeforeClzs=null;
		Class<? extends Annotation> threadGroupAfterClzs=null;
		Class<? extends Annotation> threadAllBeforeClzs=null;
		Class<? extends Annotation> threadAllAfterClzs=null;
		Class<? extends Annotation> threadForClzs=null;
		
		threadBeforeClzs=ThreadBefore.class;
		threadAfterClzs=ThreadAfter.class;
		threadGroupBeforeClzs=ThreadGroupBefore.class;
		threadGroupAfterClzs=ThreadGroupAfter.class;
		threadAllBeforeClzs=ThreadAllBefore.class;
		threadAllAfterClzs=ThreadAllAfter.class;
		threadForClzs=ThreadFor.class;
		
		List<ThreadMethodGroup> threadMethodGroupList=methodContainer.getThreadGroups();
		
		List<IThreadMethod> threadBeforeList=methodContainer.getThreadMethods(threadBeforeClzs);
		List<IThreadMethod> threadAfterList=methodContainer.getThreadMethods(threadAfterClzs);
		List<IThreadMethod> threadGroupBeforeList=methodContainer.getThreadMethods(threadGroupBeforeClzs);
		List<IThreadMethod> threadGroupAfterList=methodContainer.getThreadMethods(threadGroupAfterClzs);
		List<IThreadMethod> threadAllBeforeList=methodContainer.getThreadMethods(threadAllBeforeClzs);
		List<IThreadMethod> threadAllAfterList=methodContainer.getThreadMethods(threadAllAfterClzs);
		List<IThreadMethod> threadForList=methodContainer.getThreadMethods(threadForClzs);
		
		List<IThreadMethod> allMethodList=null;
		
		for(ThreadMethodGroup threadGroupforList:threadMethodGroupList) {
			List<IThreadMethod> groupforList = new ArrayList<IThreadMethod>();
			List<ThreadMethod> temp= threadGroupforList.getThreadmethodList();
			if (temp != null) {
				for (int k = 0; k < temp.size(); k++) {
					groupforList.add(k, temp.get(k));
				}
			}
			
			allMethodList=listJoin(allMethodList,groupforList);
		}
		
		allMethodList=listJoin(allMethodList,threadBeforeList);
		allMethodList=listJoin(allMethodList,threadAfterList);
		allMethodList=listJoin(allMethodList,threadGroupBeforeList);
		allMethodList=listJoin(allMethodList,threadGroupAfterList);
		allMethodList=listJoin(allMethodList,threadAllBeforeList);
		allMethodList=listJoin(allMethodList,threadAllAfterList);
		allMethodList=listJoin(allMethodList,threadForList);
		
		return allMethodList;
		
	}
	
	public List<IThreadMethod> listJoin(List<IThreadMethod> wantedList,List<IThreadMethod> listToJoin) {
		
		if (listToJoin.size() == 0) {
			return wantedList;
		} else {
			for (int i = 0; i < listToJoin.size(); i++) {
				if (wantedList == null) {
					wantedList=listToJoin;
				} else if (wantedList.contains(listToJoin.get(i))) {
					continue;
				} else {
					wantedList.add(listToJoin.get(i));
				}
			}
			return wantedList;
		}
	}
}
